package org.sixtynine.stock.controller;

import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.sixtynine.stock.Constants.CommonData;
import org.sixtynine.stock.dto.GridPramDTO;
import org.sixtynine.stock.dto.Page;
import org.sixtynine.stock.entity.BaseEntity;
import org.sixtynine.stock.entity.Company;
import org.sixtynine.stock.entity.IntradayShareData;
import org.sixtynine.stock.model.LoadingSearchCriteria;
import org.sixtynine.stock.service.GenericService;
import org.sixtynine.stock.service.IntradayShareDataService;
import org.sixtynine.stock.service.content.DataManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Thilan
 * 
 * this class use to add ,  edit and delete intraday share data
 *
 */

@Controller
@SessionAttributes
public class IntraDayShareDataController {

	/** TODO configure */
	private int RECORDS_PER_PAGE = 10;
	
	@Autowired
	private GenericService genericService;
	
	@Autowired
	IntradayShareDataService intradayShareDataService;
	
	@Autowired
	DataManagerService dataManagerService;
	
	@RequestMapping(value = "/intradaysharedata/search")
	public ModelAndView search(@ModelAttribute LoadingSearchCriteria criteria) {
		ModelAndView modelAndView = new ModelAndView("intradaysharedata/list");
		loadData(1, -1, criteria.getCompanyId(), criteria.getFromDate(),
				criteria.getToDate(), modelAndView, criteria);

		return modelAndView;
	}

	@RequestMapping(value = "/intradaysharedata/add")
	public ModelAndView addIntradayShareData() {
		ModelAndView modelAndView = new ModelAndView("intradaysharedata/add");
		modelAndView.addObject("intradayShareData", new IntradayShareData());

		List<BaseEntity> companies = genericService.findAll(Company.class);
		modelAndView.addObject("companyMap", companies);

		return modelAndView;
	}

	@RequestMapping(value = "/intradaysharedata/add/process")
	public ModelAndView addingIntradayShareData(@ModelAttribute @Valid IntradayShareData intradayShareData,
			BindingResult result) {

		if (!result.hasErrors()) {
			genericService.saveOrUpdate(intradayShareData);
			ModelAndView modelAndView = new ModelAndView("/intradaysharedata/list");

			List<BaseEntity> intradayShareDataList = genericService.findAll(IntradayShareData.class);
			modelAndView.addObject("intradayShareDataList", intradayShareDataList);
			return modelAndView;
		} else {
			ModelAndView modelAndView = new ModelAndView("intradaysharedata/add");
			List<BaseEntity> companies = genericService.findAll(Company.class);
			modelAndView.addObject("companyMap", companies);
			return modelAndView;
		}

	}

	@RequestMapping(value = "/intradaysharedata/list")
	public ModelAndView listOfIntradayShareData(
			@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "totalRecords", required = false) Integer totalRecords) {
		ModelAndView modelAndView = new ModelAndView("/intradaysharedata/list");
		loadData(page, totalRecords, -1, null, null, modelAndView, null);

		return modelAndView;
	}
	
	/**
	 * load data
	 * 
	 * @param page
	 * @param totalRecords
	 * @param companyId
	 * @param fromDate
	 * @param toDate
	 * @param modelAndView
	 */
	private void loadData(Integer page, Integer totalRecords, int companyId,
			Date fromDate, Date toDate, ModelAndView modelAndView,
			LoadingSearchCriteria criteria) {
		GridPramDTO gridPramDTO = new GridPramDTO();
		gridPramDTO.setRows(RECORDS_PER_PAGE);
		if (page != null) {
			gridPramDTO.setPage(page);
		} else {
			gridPramDTO.setPage(1);
		}
		if (totalRecords != null) {
			gridPramDTO._setTotalNumberOfRecords(totalRecords);
		}

		Page<IntradayShareData> intradayShareDataPage = intradayShareDataService
				.searchIntradayShareData(companyId, fromDate, toDate, gridPramDTO);
		modelAndView.addObject("intradayShareDataPage", intradayShareDataPage);

		List<BaseEntity> compaies = dataManagerService
				.getData(CommonData.COMPANY.toString());

		modelAndView.addObject("companyMap", compaies);
		if (criteria == null) {
			criteria = new LoadingSearchCriteria();

			criteria.setCompanyId(companyId);
			criteria.setFromDate(fromDate);
			criteria.setToDate(toDate);
		}
		modelAndView.addObject("criteria", criteria);

	}

	@RequestMapping(value = "/intradaysharedata/edit/{id}", method = RequestMethod.GET)
	public ModelAndView editIntradayShareData(@PathVariable Integer id) {
		ModelAndView modelAndView = new ModelAndView("/intradaysharedata/edit");
		BaseEntity intradayShareData = genericService.findById(id, IntradayShareData.class);
		modelAndView.addObject("intradayShareData", intradayShareData);
		List<BaseEntity> companies = genericService.findAll(Company.class);
		modelAndView.addObject("companyMap", companies);
		return modelAndView;
	}

	@RequestMapping(value = "/intradaysharedata/edit/{id}", method = RequestMethod.POST)
	public ModelAndView edditingIntradayShareData(@ModelAttribute @Valid IntradayShareData intradayShareData,
			BindingResult result , @PathVariable Integer id) {
		if (!result.hasErrors()) {
			
			genericService.saveOrUpdate(intradayShareData);
			ModelAndView modelAndView = new ModelAndView("/intradaysharedata/list");
			String message = "Team was successfully edited.";
			modelAndView.addObject("message", message);
	
			return modelAndView;
		}
		else{
			ModelAndView modelAndView = new ModelAndView("/intradaysharedata/edit");
			List<BaseEntity> companies = genericService.findAll(Company.class);
			modelAndView.addObject("companyMap", companies);
			return modelAndView;
		}
	}

	@RequestMapping(value = "/intradaysharedata/delete/{id}", method = RequestMethod.GET)
	public ModelAndView deleteIntradayShareData(@PathVariable Integer id) {
		ModelAndView modelAndView = new ModelAndView("/intradaysharedata/list");
		genericService.delete(id, IntradayShareData.class);
		String message = "Team was successfully deleted.";
		modelAndView.addObject("message", message);
		return modelAndView;
	}
	
}
